$(function(){
    $('nav#menu_left').mmenu();
    $('#toJoin').on('click', function(e){
        var btn = $(this);
        attendAct(btn);
    });
    $('#toCancel').on('click', function(e){
        AlertBox.showConfirm('<br/>亲，真的要走吗？', {ok:'嗯，有事',cancel:'留下吧'}, function(){
            var btn = $(this);
            btn.button('loading');
            $.post('/cancel', {id:actId}, function(res){
                if(!res.error){
                    if(res.data == null){
                        AlertBox.showTip('还未参与!');
                    }else{
                        AlertBox.showTip('下次再去吧√');
                        window.location.href = '';
                    }
                }else{
                    AlertBox.showTip('取消失败×')
                }
                btn.button('reset');
            })
        }, function(){
            AlertBox.closeAlertBox();
        })
    });
    $('#scoreListBtn').on('click', function(e){
        window.location.href = '/actScoreList/'+actId
    });
    $('#loadMore').on('click', function(e){
        var btn = $(this);
        $('.hide_more').each(function(i){
            btn.remove();
            $(this).fadeIn();
        })
    });
    $('#deseShow').on('click', function(e){
        window.location.href = '/actScoreList/'+actId+'/'+score+'/'+no;
    })
    $('#joinFactionBtn').on('click', function(e){
        $.post('/faction/join',
            {factionId:factionId, from: from}, function(res){
                if(!res.error){
                    $('#joinfactionText').html('<h1>欢迎加入圈子！</h1>');
                    $('#joinFaction').modal('hide');
                    //自动触发签到
                    $('#toJoin').trigger('click');
                }else{
                    AlertBox.showTip('加入失败!');
                }
            })
    })
});
function _showGift(){
    $('#gift').modal('show');
}
function attendAct(btn){
    btn.button('loading');
    var faction = $('#factionId').val();
    $.post('/apply', {id:actId, remark: '我来啦!',faction: faction, from: from}, function(res){
        if(!res.error){
            if(res.data === 'already'){
                AlertBox.showTip('已经报到了~');
                //window.location.href = '/actScoreList/'+actId;
                //_showGift();
            }else
            if(res.data === 'outside'){
                //AlertBox.showTip('非此圈内人无法参加');
                $('#joinFaction').modal('show');
            }else{
                AlertBox.showTip('参与成功√');
                showAttend(res.data);
                window.location.href = '/actScoreList/'+actId+'/'+res.data.score+'/'+res.data.No;
                //_showGift();
            }
            btn.button('reset');
        }
    });
}
function showAttend(attend){
    if($('#attendlist').css('display') === 'none'){
        $('#attendlist').css('display', 'block')
    }
    var html = '<li><img src="/img/'+attend.id+'" class="img-rounded size_32"> '+attend.name+'</li><hr>';
    $('#attendlist ul').prepend(html);
}